package ru.usu.gv.gui.analysis;

import ru.usu.gv.core.GraphCanvas;
import ru.usu.gv.graph.Graph;
import ru.usu.gv.styles.StyleFacade;
import ru.usu.gv.uicomponents.UIGraph;
import ru.usu.gv.uicomponents.UIVertex;
import ru.usu.gv.utils.GraphAlgorithmHelper;

import java.util.List;

/**
 @author Sergey Pupyrev 
 12.04.2007
 */

public class VertexDistanceAnalyzer extends GraphAnalyzer
{
	public VertexDistanceAnalyzer()
	{
		super(AnalysisPanel.getInstance().getVertexDistanceSlider());
	}

	@Override
	protected double calculateAnalysValue()
	{
		UIGraph uiGraph = GraphCanvas.getInstance().getCurrentGraph();
		Graph graph = uiGraph.getGraph();

		double standardLength = StyleFacade.getEdgeLength();
		int n = graph.getSize();
		int[][] a = new int[n][n];
		GraphAlgorithmHelper.initializeFloydMatrix(a);

		int countEdges = 0;
		double error = 0.0;
		List<UIVertex> vertexList = uiGraph.getVertexes();
		for (int i = 0; i < vertexList.size(); i++)
			for (int j = i + 1; j < vertexList.size(); j++)
			{
				double len = vertexList.get(i).getPosition().distance(vertexList.get(j).getPosition());
				int graphDistance = a[i][j];
				if (graphDistance != -1)
				{
					double sl = standardLength * graphDistance;
					error += Math.abs(len - sl) / sl;
					countEdges++;
				}
			}

		error /= countEdges;
		return 1.0 - error;
	}

}
